<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>菜单界面</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/bootstrap/css/bootstrap.min.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/mobile.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/icon.css"/>">
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.easyui.mobile.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/js/common.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/jsp-mobile/cashier/cashierForCust.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/bootstrap/js/bootstrap.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/js/LodopFuncs.js"/>"></script>
<script language="JavaScript">
	var addId = 1;
	function keytimes(date1, date2) {/*計算2時間毫秒差*/
		var keyw = '-';
		var str1 = date1;
		var str2 = date2;
		var array1 = str1.split(keyw);
		var array2 = str2.split(keyw);

		var Y1 = array1[0];
		var M1 = array1[1];
		var D1 = array1[2];
		var H1 = array1[3];
		var I1 = array1[4];
		var S1 = array1[5];
		var L1 = array1[6];
		var Y2 = array2[0];
		var M2 = array2[1];
		var D2 = array2[2];
		var H2 = array2[3];
		var I2 = array2[4];
		var S2 = array2[5];
		var L2 = array2[6];

		var d1 = new Date();
		d1.setYear(Y1);
		d1.setMonth(M1, D1);
		d1.setHours(H1);
		d1.setMinutes(I1);
		d1.setSeconds(S1);
		d1.setMilliseconds(L1);
		var d2 = new Date();
		d2.setYear(Y2);
		d2.setMonth(M2, D2);
		d2.setHours(H2);
		d2.setMinutes(I2);
		d2.setSeconds(S2);
		d2.setMilliseconds(L2);
		return (d2.getTime() - d1.getTime());
	}

	function getNowTime() {/*獲取當前時間,精確到毫秒*/
		var now = new Date();
		var year = now.getYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		var second = now.getSeconds();
		var milSecond = now.getMilliseconds();
		//var myweekday=now.getDay();
		return year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-"
				+ second + "-" + milSecond;
	}

	function checkinput(keyCode, boxid, htxtid1, htxtid2) {
		var okvalue
		if (keyCode == 13) {
			if (document.getElementById(htxtid1).value == ''
					|| document.getElementById(htxtid2).value == '') {
				alert("輸入數據錯誤?");
				document.getElementById(boxid).value = '';
				document.getElementById(htxtid1).value = '';
				document.getElementById(htxtid2).value = '';
				return false;
			}
			okvalue = keytimes(document.getElementById(htxtid1).value, document
					.getElementById(htxtid2).value);

			document.getElementById(htxtid1).value = '';
			document.getElementById(htxtid2).value = '';
		} else {
			if (document.getElementById(htxtid1).value != '') {
				if (document.getElementById(htxtid2).value != '') {
					document.getElementById(htxtid2).value = getNowTime();
				} else {
					document.getElementById(htxtid2).value = getNowTime();
				}
			} else {
				document.getElementById(htxtid1).value = getNowTime();
			}
			if (document.getElementById(boxid).value == '') {
				document.getElementById(htxtid1).value = '';
				document.getElementById(htxtid2).value = '';
			}
			okvalue = '';
		}
		return okvalue;
	}
	function SubmitKeyClickrepint(obj, evt, putid) {
		evt = (evt) ? evt : ((window.event) ? window.event : "");
		keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which
				: evt.charCode);

		/* var aa = checkinput(keyCode, putid, 'checktime1', 'checktime2');
		if (!aa) {
			return false;
		}
		document.getElementById('shownums').innerHTML = aa; */
		/* if (aa != '' && aa > 150) {
			alert('禁止手動輸入');
			document.getElementById(putid).value = '';
		} else  */
		if (keyCode == 13) {
			/*放入執行語句*/

			//查询是否有相同条形码商品
			var returnFlag = queryMater(document.getElementById(putid).value,
					putid);
			if (returnFlag == "newDate") {
				//生成下调语句
				$('#dg').datagrid('appendRow', {
					itemid : addId
				});
				document.getElementById(addId.toString()).focus();
				addId = addId + 1;
			} else if (returnFlag == "oldDate") {
				document.getElementById(putid).value = '';
				document.getElementById(putid).focus();
			} else {
				document.getElementById(putid).blur();
				alert("查询不到条码");
			}

			//更新总价钱
			computeMaterValueAll();

		}

	}

	function queryMater(barcode, putId) {
		var returnflag = false;
		$
				.ajax({
					async : false,
					url : "<c:url value='/cashier/getMaterByBarcode.do'/>"
							+ "?barcode=" + barcode,
					type : "post",
					dataType : "json",
					success : function(data) {
						if (data.success) {
							//查询是否已经录入商品
							returnflag = "newDate";
							var rowDateAllCheck = $('#dg').datagrid('getRows');
							var newbarcode = document.getElementById(putId).value;
							for (var i = 0; i < rowDateAllCheck.length; i++) {
								if (document
										.getElementById(rowDateAllCheck[i]['itemid']).value == newbarcode
										&& rowDateAllCheck[i]['itemid'] != putId) {
									document.getElementById(putId).value = '';
									putId = rowDateAllCheck[i]['itemid']
									returnflag = "oldDate";
								}
							}

							//设置行属性
							var rowIndex = $('#dg').datagrid('getRowIndex',
									putId);
							var rowDateAll = $('#dg').datagrid('getRows');
							for (var i = 0; i < rowDateAll.length; i++) {
								if (rowIndex == $('#dg').datagrid(
										'getRowIndex', rowDateAll[i]['itemid'])) {

									if (returnflag == "newDate") {
										rowDateAll[i]['barcode'] = barcode;
										rowDateAll[i]['materId'] = data.materId;
										rowDateAll[i]['materName'] = data.materName;
										rowDateAll[i]['materValue'] = data.materValue;
										rowDateAll[i]['materValueAll'] = data.materValue;

										rowDateAll[i]['unit'] = data.unit;

										$('#dg').datagrid('refreshRow', i);
										$('#dg')
												.datagrid('beginEdit', rowIndex);
										document.getElementById(putId).value = barcode;
										document.getElementById("num" + putId).value = 1;
									} else if (returnflag == "oldDate") {
										var materNum = document
												.getElementById("num" + putId).value * 1 + 1;
										rowDateAll[i]['materValueAll'] = decimal(
												data.materValue * materNum, 2);
										$('#dg').datagrid('refreshRow', i);
										$('#dg')
												.datagrid('beginEdit', rowIndex);
										document.getElementById(putId).value = barcode;
										document.getElementById("num" + putId).value = materNum;
									}

								}
							}

						}
					}
				});
		return returnflag;
	}

	//列表
	var data = [];
	$(function() {
		$('#dg').datagrid({
			data : data,
			showFooter : true,
			onLoadSuccess : function() {
				$('#dg').datagrid('appendRow', {
					itemid : addId
				});
				addId = addId + 1;

				$('#dg').datagrid('reloadFooter', [ {
					materId : -1,
					materValue : '合计',
					materValueAll : 0
				} ]);
			},
			onClickRow : function(rowIndex) {
				//alert(rowIndex);
				$('#dg').datagrid('beginEdit', rowIndex);
			}
		});
	});

	//操作
	function removeit() {

		var row = $('#dg').datagrid('getSelections');
		if (row.length == 0) {
			$.messager.alert('提示', '请选择一条记录操作！', 'info');
			return;
		} else {

		}
		$('#dg').datagrid('clearSelections');
	}

	function computeMaterValueAll() {
		var rowDateAll = $('#dg').datagrid('getRows');

		var computematerValueAll = 0;
		for (var i = 0; i < rowDateAll.length; i++) {
			if (rowDateAll[i]['materValueAll'] != ''
					&& rowDateAll[i]['materValueAll'] != 0
					&& rowDateAll[i]['materValueAll'] != undefined) {
				computematerValueAll = computematerValueAll
						+ rowDateAll[i]['materValueAll'];
			}
		}
		$('#dg').datagrid('reloadFooter', [ {
			materId : -1,
			materValue : '合计',
			materValueAll : computematerValueAll
		} ]);

	}
	function formatInputbox(value, rowData, rowIndex) {
		var valueString = '';
		if (rowData['materId'] != -1) {
			valueString = "<input type='text' name='"
					+ rowData['itemid']
					+ "' id='"
					+ rowData['itemid']
					+ "' style='width:99%;height: 26px;margin: 0;' onKeyUp=\"return SubmitKeyClickrepint(this,event,'"
					+ rowData['itemid'] + "')\" />";
		}
		return valueString.toString();
	}

	function formatInputboxNum(value, rowData, rowIndex) {
		var valueString = '';
		if (rowData['materId'] != -1) {
			valueString = "<input type='text' name='num"
					+ rowData['itemid']
					+ "' id='num"
					+ rowData['itemid']
					+ "' style='width:99%;height: 26px;margin: 0;' onKeyUp=\"return numInputOnKeyUp(this,event,'num"
					+ rowData['itemid'] + "')\" />";
		}
		return valueString.toString();
	}
</script>
</head>

<body oncut="return false" onpaste="return false">
	<table id="dg" idField="itemid"
		data-options="
                header:'#hh',
                iconCls: 'icon-edit',
                singleSelect: true,
                fit:false,
                fitColumns:true,
                border: false,
                scrollbarSize: 0
            ">
		<thead>
			<tr>
				<th data-options="field:'itemid',width:20">序号ID</th>
				<th
					data-options="field:'barcode',width:50,formatter:formatInputbox,">条形码</th>
				<th
					data-options="field:'materNum',width:50,align:'right',formatter:formatInputboxNum">货品单位数量</th>
				<th data-options="field:'unit',width:50,align:'left'">单位</th>
				<th data-options="field:'materId',width:50,align:'left',hidden:true">货品id</th>
				<th data-options="field:'materName',width:50,align:'left'">货品名称</th>
				<th data-options="field:'materValue',width:50,align:'left'">货品单价</th>
				<th data-options="field:'materValueAll',width:50,align:'left'">货品总价</th>
			</tr>
		</thead>
	</table>
	<!-- <table>
		<tr>
			<input type="text" name="OK" id="OK"
				onKeyUp="return SubmitKeyClickrepint(this,event,'OK')" />
		</tr>
		<tr>
			<input type="text" name="checktime1" id="checktime1" />
		</tr>
		<tr>
			<input type="text" name="checktime2" id="checktime2" />
		</tr>
		<tr>
			<span id="shownums"
				style="color: red; font-weight: 900; font-size: 60px;"></span>
		</tr>
	</table>
	 -->
	<div id="hh">
		<div class="m-toolbar">
			<div class="m-title">Shopping cart</div>
			<div class="m-right">
				<a href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-remove',plain:true"
					onclick="removeit()"></a> <a href="javascript:void(0)"
					class="easyui-linkbutton"
					data-options="iconCls:'icon-save',plain:true" onclick="accept()"></a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-undo',plain:true" onclick="reject()"></a>
			</div>
		</div>
	</div>

	<object id="LODOP_OB"
		classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
		<embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
	</object>
	<form id="searchForm" name="searchForm" method="post"
		action="userlist.txt"
		onsubmit="return searchDGData(this.id,'listDisplay');">
		<div style="width: 100%; text-align: center;">
			<table style="width: 50%; text-align: center; margin: auto">
				<tr>
					<td>
						<button type="button" onclick="prn1_print()" class="btn btn-danger">打印</button>
					</td>
					<td>
						<button type="button" onclick="prn1_view()" class="btn btn-danger">打印预览</button>
					</td>
					<td>
						<button type="button" class="btn btn-danger">危险按钮</button>
					</td>
					<td>
						<button type="button" class="btn btn-danger">危险按钮</button>
					</td>
					<td>
						<button type="button" class="btn btn-danger">危险按钮</button>
					</td>
				</tr>
			</table>
		</div>
	</form>


	<form id="excelForm" name="excelForm" method="post"
		enctype="multipart/form-data" action="userlist.txt"
		onsubmit="return searchDGData(this.id,'listDisplay');">
		<div>
			<table cellpadding="5">
				<tr>

					<td>111</td>
					<td>222</td>
					<td>333</td>
				</tr>
			</table>
		</div>
	</form>



</body>
<style scoped>
body {
	padding: 0; /*去除内边距*/
	border: 0; /*去除边框*/
	margin: 0; /*去除外边距*/
}
</style>
</html>